package cn.kgc.jdbc.dao;

import cn.kgc.jdbc.entity.Clazz;
import cn.kgc.jdbc.utils.DruidUtil;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author: mosin
 * @version: v1.0  2022/8/28
 */
public class ClazzDaoImpl implements ClazzDao {

    @Override
    public Clazz findByClazzName(String clazzName) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select * from clazz where clazz_name = ?";

        //实现驼峰命名的转换器
        BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
        try {
            Clazz clazz = queryRunner.query(sql, new BeanHandler<Clazz>(Clazz.class,basicRowProcessor), clazzName);
            return  clazz;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }
}
